home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Tool Chest / Dev.CD Feb 97 TC.toast / Sample Code / Files / Folder Watching / Folder Watcher FBA ƒ / Sources / FBA.c next >
Encoding:
C/C++ Source or Header  |  1996-02-16  |  5.3 KB  |  241 lines  |  [TEXT/CWIE]

  1. // FBA.c
  2. //
  3. // Folder Watcher FBA by Greg Sutton
  4. // Base code for this FBA by Chris White
  5. // ©Apple Computer Inc 1996, all rights reserved.
  6.  
  7.  
  8.     // Includes
  9. #include "FBA.h"
  10. #include "FBAAppleEvents.h"
  11. #include "FBATask.h"
  12. #include "FBALists.h"
  13.  
  14. #include <GestaltEqu.h>
  15. #include <LowMem.h>
  16. #include <Events.h>
  17. #include <Notification.h>
  18. #include <Resources.h>
  19.  
  20. #ifdef DEVELOPMENT
  21.     #include <TextEdit.h>
  22.     #include <Dialogs.h>
  23. #endif
  24.  
  25.  
  26.     // Prototypes
  27. static Boolean            HasFullExtFSDispatching( void );
  28. static Boolean            HasProcessManager( void );
  29. static void                InitCycleTime( void );
  30. static unsigned long    GetResourceCycleTime( void );
  31.  
  32. static void                CheckEventQueue( void );
  33.  
  34. static void                Notify( StringPtr notice, Boolean fFatal );
  35.  
  36. static pascal void        MyResponse( NMRecPtr n );
  37.  
  38.  
  39.     // Globals
  40. #if !defined(THINK_C) && !defined(__MWERKS__)
  41.         QDGlobals       qd;
  42. #endif
  43.  
  44. short             gQuit = false;
  45. unsigned long    gCycleTime;            // Time requested to check all folders
  46.  
  47.     // Constants
  48. const unsigned long    DefaultCycleTime = 30;    // Default cycle time for all folders
  49.                                             // to be checked in seconds.
  50. const short            StackSize = 32;            // Default stack size for an FBA is 2K
  51.                                             // Set StackSize to the K wanted.
  52.  
  53.  
  54. void    main( void )
  55. {
  56.     long            numBytes,
  57.                     newLimit;
  58.     Boolean            fQuit;
  59.     
  60.                 // set the stack size
  61.      numBytes = StackSize * 1024;
  62.                 // calculate the absolute memory byte by
  63.                 // offsetting the stack base
  64.      newLimit = (long)LMGetCurStackBase( ) - numBytes;
  65.                 // check that there is enough memory for stack increase
  66.      if ((long)GetApplLimit( ) > newLimit)
  67.           SetApplLimit( (Ptr)newLimit );
  68.     
  69.     InitGraf( &qd.thePort );
  70.  
  71. #ifdef DEVELOPMENT    // See 'FBA.h' for #define
  72.     InitFonts( );
  73.     InitWindows( );
  74.     InitMenus( );
  75.     TEInit( );
  76.     InitDialogs( 0L );
  77.     InitCursor( );
  78. #endif
  79.  
  80.     if ( noErr != InitAppleEvents( ) )
  81.         Notify( "\pFolder Watcher failed to initialize Apple events", kFatalErr );
  82.     
  83.     if ( ! HasFullExtFSDispatching( ) )
  84.         Notify( "\pFolder Watcher failed due to limited CatSearch", kFatalErr );
  85.     
  86.     if ( ! HasProcessManager( ) )
  87.         Notify( "\pFolder Watcher failed due to no Process Manager", kFatalErr );
  88.         
  89.     if ( ! InitTask( ) )
  90.         Notify( "\pFolder Watcher failed due to bad task initialization", kFatalErr );
  91.  
  92.     InitWatchFolders( );
  93.     InitCycleTime( );
  94.     
  95.     fQuit = gQuit;                        // Initialization may set gQuit to true
  96.     
  97.     while ( ! fQuit )
  98.     {
  99.         CheckEventQueue( );
  100.         
  101.         if ( gQuit )                    // We want to quit
  102.             fQuit = CanQuitTask( );        // Check that our task can quit now
  103.     }
  104. }
  105.  
  106.  
  107. static void    CheckEventQueue( void )
  108. {
  109.     long                    sleepTicks = 1;    // First time through return from
  110.                                             //  WaitNextEvent() as quick as possible.
  111.     EventRecord             anEvent;
  112.     OSErr                    err;
  113.  
  114.     WaitNextEvent( highLevelEventMask, &anEvent, sleepTicks, NULL );
  115.     
  116.     switch ( anEvent.what )
  117.     {
  118.         case kHighLevelEvent :
  119.             err = DoAppleEvent( &anEvent );
  120.         break;
  121.     }
  122.     
  123.         // Call background task no matter if event
  124.         //  is a high level or null event.
  125.     sleepTicks = DoBackgroundTask( );        // Do our background task or return
  126.                                             //  an updated sleep time.
  127. }
  128.  
  129.  
  130. // See if File Manager will pass CatSearch requests to external file systems.
  131.  
  132. static Boolean    HasFullExtFSDispatching( void )
  133. {
  134.     long        response;
  135.     Boolean        result = false;
  136.  
  137.     if ( noErr == Gestalt( gestaltFSAttr, &response ) )
  138.         result = ((response & (1L << gestaltFullExtFSDispatching)) != 0);
  139.         
  140.     return result;
  141. }
  142.  
  143.  
  144. // Check that we have the process manager
  145.  
  146. static Boolean    HasProcessManager( void )
  147. {
  148.     long        response;
  149.     Boolean        result = false;
  150.  
  151.     if ( noErr == Gestalt( gestaltOSAttr, &response ) )
  152.         result = true;
  153.         
  154.     return result;
  155. }
  156.  
  157.  
  158. static void    InitCycleTime( void )
  159. {
  160.     gCycleTime = GetResourceCycleTime();
  161. }
  162.  
  163.  
  164. unsigned long    GetCycleTime( void )
  165. {
  166.     return gCycleTime;
  167. }
  168.  
  169.  
  170. // If there are 'Cycl' resources then use the first of these for the sleep time.
  171. // Otherwise use the default cycle time. The time is supposed to be in seconds.
  172.  
  173. static unsigned long GetResourceCycleTime( void )
  174. {
  175.     short            count;
  176.     Handle             aHandle;
  177.     unsigned long    result = DefaultCycleTime;
  178.     
  179.     count = Count1Resources( kCycleTimeResource );
  180.     if ( count )
  181.     {
  182.         aHandle = Get1IndResource( kCycleTimeResource, 1 );
  183.         if ( aHandle )
  184.         {
  185.             result = *(unsigned long *) *aHandle;
  186.             ReleaseResource( aHandle );
  187.         }
  188.     }
  189.     
  190.     return result;
  191. }
  192.  
  193.  
  194.  
  195. static pascal void MyResponse ( NMRecPtr n )
  196. {
  197.     DisposePtr ( (Ptr) n->nmStr );
  198.     NMRemove ( n );
  199.     if ( ! n->nmRefCon )        // true if error was fatal
  200.         DisposePtr ( (Ptr) n );
  201.     else
  202.         n->nmRefCon = 0L;
  203.     
  204.     return;
  205. }
  206.  
  207.  
  208. // Use the Notification manager to display a message.
  209. // Pass true to fFatal if the applcation is to quit afterwards.
  210.  
  211. void    Notify( StringPtr notice, Boolean fFatal )
  212. {
  213.     // remember, no user interface to play with
  214.     NMRecPtr    notePtr;
  215.     OSErr        err;
  216.     
  217.     notePtr = (NMRecPtr) NewPtr ( sizeof ( NMRec ) );
  218.     if ( MemError ( ) )
  219.         return;
  220.     
  221.     notePtr->qType = nmType;            // standard queue type for NM
  222.     notePtr->nmMark = 0;                // background only
  223.     notePtr->nmIcon = 0L;
  224.     notePtr->nmSound = (Handle) -1L;    // use system alert snd
  225.     notePtr->nmStr = NULL;
  226.     notePtr->nmStr = (StringPtr) NewPtr ( sizeof ( Str255 ) );
  227.     BlockMoveData( notice, notePtr->nmStr, notice[0] + 1 );
  228.     notePtr->nmResp = NewNMProc( MyResponse );
  229.     notePtr->nmRefCon = fFatal;
  230.     
  231.     err = NMInstall ( notePtr );
  232.     if ( err == noErr && fFatal )
  233.     {
  234.         while ( notePtr->nmRefCon )
  235.             CheckEventQueue( );
  236.         gQuit = true;
  237.     }
  238.  
  239.     return;
  240. }
  241.